package com.gosophia.conferenceManagement.summary.ws.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.DozerChangeUtil;
import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.exception.ConcurrencyException;
import com.gosophia.commons.exception.ParameterException;
import com.gosophia.commons.exception.TechnicalException;
import com.gosophia.conferenceManagement.common.dto.DocumentDTO;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.common.ws.ConferenceQueryWebService;
import com.gosophia.conferenceManagement.common.ws.impl.ConferenceHandleWebServiceImpl;
import com.gosophia.conferenceManagement.summary.dto.SummaryJobDataDTO;
import com.gosophia.conferenceManagement.summary.entity.SummaryJobData;
import com.gosophia.conferenceManagement.summary.service.SummaryService;
import com.gosophia.conferenceManagement.summary.ws.SummaryWebService;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.service.TaskManagementService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.entity.UserInfo;
import com.gosophia.userManagement.service.UserManagementService;

@Service("summaryWebService")
@Transactional
public class SummaryWebServiceImpl extends ConferenceHandleWebServiceImpl implements SummaryWebService{

    @Autowired
    private TaskManagementService taskManagementService;
    @Autowired
    private UserManagementService userManagementService;
    @Autowired
    private ConferenceQueryWebService conferenceQueryWebService;
    
    @Autowired
    private SummaryService summaryService;
    
    private static final String JOB_TYPE="总结审批";
    
    private Mapper dozer = new DozerBeanMapper();
    
    /**
     * 根据jobId查询上一个SummaryJobData
     * @param jobId
     * jobId不能为null
     * @return SummaryJobDataDTO  没有指定jobId的实体时返回null
     * @throws BusinessException 
     */
    @Override
    public SummaryJobDataDTO findPreSummaryJobDataByJobDataId(Long jobId) throws BusinessException {
        Assert.notNull(jobId,"jobId不能为null");
        Job job = taskManagementService.findJobDetailByJobId(jobId);
        if(job==null){
            return null;
        }
        Job previousJob = job.getPreviousJob();
        if(previousJob==null){
            return null;
        }
        SummaryJobData summaryJobData = (SummaryJobData) taskManagementService.findJobDataDetail(previousJob.getJobId(), SummaryJobData.class);
        
        SummaryJobDataDTO summaryJobDataDTO =null;
        if(summaryJobData!=null){
            
            summaryJobDataDTO= DozerChangeUtil.changeBeforeToAfter(summaryJobData, SummaryJobDataDTO.class);
            
            //处理人
            if(summaryJobData.getNextApproval()!=null){
                UserInfoDTO nextApprovalDTO = DozerChangeUtil.changeBeforeToAfter(summaryJobData.getNextApproval(), UserInfoDTO.class);
                summaryJobDataDTO.setNextApproval(nextApprovalDTO);
            }
            //在线文档
            if(summaryJobData.getOnlineDocument()!=null){
                DocumentDTO onlineDocumentDTO = DozerChangeUtil.changeBeforeToAfter(summaryJobData.getOnlineDocument(), DocumentDTO.class);
                summaryJobDataDTO.setOnlineDocumentDTO(onlineDocumentDTO);
            }
            //附件
            List<DocumentDTO> attachmentsDTOs = new ArrayList<DocumentDTO>();
            if(summaryJobData.getAttachments()!=null && summaryJobData.getAttachments().size()!=0){
                for(Document d:summaryJobData.getAttachments()){
                    DocumentDTO attachmentDTO = DozerChangeUtil.changeBeforeToAfter(d, DocumentDTO.class);
                    attachmentsDTOs.add(attachmentDTO);
                }
                summaryJobDataDTO.setAttachmentsDTOs(attachmentsDTOs);
            }
        }
        return summaryJobDataDTO;
    }

    /**
     * 根据jobId查询指定会议总结的审批的历史记录
     * @param jobId
     * jobId不能为null
     * @return List<JobDTO>  如果没有历史记录则返回null
     * @throws BusinessException  
     */
    @Override
    public List<JobDTO> findSummaryHistoryByJobId(Long jobId) throws BusinessException {
        Assert.notNull(jobId,"jobId不能为null");
        Job job = taskManagementService.findJobDetailByJobId(jobId);
        List<Job> jobs = new ArrayList<Job>();
        List<JobDTO> jobDTOs = new ArrayList<JobDTO>();
        if(job!=null){
            jobs = taskManagementService.findJobByMissionTypeAndJobTypeFinished(job.getMission().getMissionId(), JOB_TYPE);
            for(Job j : jobs){
                JobDTO jDTO = DozerChangeUtil.changeBeforeToAfter(j, JobDTO.class);
                UserInfoDTO approval = DozerChangeUtil.changeBeforeToAfter(j.getOperator(), UserInfoDTO.class);
                jDTO.setOperatorDTO(approval);
                jobDTOs.add(jDTO);
            }
        }
        return jobDTOs;
    }

    /**
     * 根据jobId查询SummaryJobData
     * @param jobId
     * jobId不能为null
     * @return SummaryJobDataDTO  没有指定jobId的实体时返回null
     * @throws BusinessException 
     */
    @Override
    public SummaryJobDataDTO findSummaryJobDataByJobDataId(Long jobId) throws BusinessException {
        Assert.notNull(jobId,"jobId不能为null");
        
        SummaryJobData summaryJobData =(SummaryJobData)taskManagementService.findJobDataDetail(jobId, SummaryJobData.class);
        
        SummaryJobDataDTO summaryJobDataDTO = null;
        if(summaryJobData!=null){
            
            summaryJobDataDTO= DozerChangeUtil.changeBeforeToAfter(summaryJobData, SummaryJobDataDTO.class);
            
            
            if(summaryJobData.getJob()!=null ){
                JobDTO jobDTO = DozerChangeUtil.changeBeforeToAfter(summaryJobData.getJob(), JobDTO.class);
                summaryJobDataDTO.setJobDTO(jobDTO);
                //处理人
                if( summaryJobData.getJob().getOperator()!=null){
                    UserInfoDTO operatorDTO = DozerChangeUtil.changeBeforeToAfter(summaryJobData.getJob().getOperator(), UserInfoDTO.class);
                    summaryJobDataDTO.getJobDTO().setOperatorDTO(operatorDTO);
                }
            }
            //在线文档
            if(summaryJobData.getOnlineDocument()!=null){
                DocumentDTO onlineDocumentDTO = DozerChangeUtil.changeBeforeToAfter(summaryJobData.getOnlineDocument(), DocumentDTO.class);
                summaryJobDataDTO.setOnlineDocumentDTO(onlineDocumentDTO);
            }
            //附件
            List<DocumentDTO> attachmentsDTOs = new ArrayList<DocumentDTO>();
            if(summaryJobData.getAttachments()!=null && summaryJobData.getAttachments().size()!=0){
                for(Document d:summaryJobData.getAttachments()){
                    DocumentDTO attachmentDTO = DozerChangeUtil.changeBeforeToAfter(d, DocumentDTO.class);
                    attachmentsDTOs.add(attachmentDTO);
                }
                summaryJobDataDTO.setAttachmentsDTOs(attachmentsDTOs);
            }
        }
        return summaryJobDataDTO;
    }

    /**
     * 修改SummaryJobData
     * @param summaryJobDataDTO
     * summaryJobDataDTO不能为null
     * jobDTO不能为null
     * jobId不能为null
     * @throws BusinessException 
     */
    @Override
    public void updateSummaryJobData(SummaryJobDataDTO summaryJobDataDTO) throws BusinessException {
        Assert.notNull(summaryJobDataDTO,"summaryJobDataDTO不能为null");
        Assert.notNull(summaryJobDataDTO.getJobDTO(),"jobDTO不能为null");
        Assert.notNull(summaryJobDataDTO.getJobDTO().getJobId(),"jobId不能为null");
        
        Long  jobId = summaryJobDataDTO.getJobDTO().getJobId();
        
        SummaryJobData summaryJobData = (SummaryJobData) taskManagementService.findJobDataDetail(jobId, SummaryJobData.class);
        if(summaryJobData!=null){
            if(summaryJobDataDTO.getNextApproval()!=null){
                UserInfo nextApproval = DozerChangeUtil.changeBeforeToAfter(summaryJobDataDTO.getNextApproval(), UserInfo.class);
                summaryJobData.setNextApproval(nextApproval);
            }
            summaryJobData.setOpinion(summaryJobDataDTO.getOpinion());
            summaryJobData.setTurningAction(summaryJobDataDTO.getTurningAction());
        }
        summaryService.updateConferenceJobData(summaryJobData);
        
    }
    
    @Override
    public void HandleConferenceTurningSummary(Long jobId, Long operatorId,
            SummaryJobDataDTO summaryJobDataDTO)
            throws BusinessException {
        Assert.notNull(jobId, "jobId不能为null");
        Assert.notNull(summaryJobDataDTO, "jobData不能为null");
        Assert.notNull(operatorId,"操作人Id不能为空");
        
        SummaryJobData summaryJobData = (SummaryJobData) taskManagementService.findJobDataDetail(jobId, SummaryJobData.class);
        Document document=null;
        if(summaryJobData!=null){
            document = summaryJobData.getOnlineDocument();
        }else{
            summaryJobData = new SummaryJobData();
            document = new Document();
        }
        
        document.setDocumentPath(summaryJobDataDTO.getOnlineDocumentDTO().getDocumentPath());
        document.setDocumentVersion(summaryJobDataDTO.getOnlineDocumentDTO().getDocumentVersion());
        summaryJobData.setOnlineDocument(document);
        
        
        if(summaryJobDataDTO.getNextApproval()!=null&&summaryJobDataDTO.getNextApproval().getUserInfoId()!=null){
             summaryJobData.setNextApproval(userManagementService.
                     findUserInfoByUserInfoId(summaryJobDataDTO.getNextApproval().getUserInfoId()));
         }
         //附件
         List<Document> attachments = new ArrayList<Document>();
         if(summaryJobDataDTO.getAttachmentsDTOs()!=null){
             for (DocumentDTO d : summaryJobDataDTO.getAttachmentsDTOs()) {
                Document doc = new Document();
                doc = dozer.map(d, Document.class);
                attachments.add(doc);
            }
         }
         summaryJobData.setAttachments(attachments);
         summaryJobData.setOpinion(summaryJobDataDTO.getOpinion());
         summaryJobData.setTurningAction(summaryJobDataDTO.getTurningAction());
         summaryService.HandleConferenceTurning(jobId, operatorId, summaryJobData);
    }

    @Override
    public void saveOrUpdateSummaryJobData(SummaryJobDataDTO summaryJobDataDTO,
            List<DocumentDTO> attachements, DocumentDTO onlineDocument)
            throws BusinessException, ParameterException, ConcurrencyException,
            TechnicalException {
        Assert.notNull(summaryJobDataDTO,"summaryJobData不能为null");
        SummaryJobData summaryJobData = DozerChangeUtil.changeBeforeToAfter(summaryJobDataDTO, SummaryJobData.class);
        //转换job
        if(summaryJobDataDTO.getJobDTO()!=null){
            Job job = taskManagementService.findJobDetailByJobId(summaryJobDataDTO.getJobDTO().getJobId());
            summaryJobData.setJob(job);
        }
        if(summaryJobDataDTO.getNextApproval()!=null){
            UserInfo nextApproval = DozerChangeUtil.changeBeforeToAfter(summaryJobDataDTO.getNextApproval(), UserInfo.class);
            summaryJobData.setNextApproval(nextApproval);
        }
        List<Document> attachements1 = new ArrayList<Document>();
        if(attachements!=null){
            for(DocumentDTO dDTO:attachements){
                Document d = new Document();
                d.setDocumentCategory(dDTO.getDocumentCategory());
                d.setDocumentName(dDTO.getDocumentName());
                d.setDocumentNote(dDTO.getDocumentNote());
                d.setDocumentPath(dDTO.getDocumentPath());
                d.setDocumentVersion(dDTO.getDocumentVersion());
                attachements1.add(d);
            }
        }
        Document onlineDocument1 = null;
        if(onlineDocument!=null){
            onlineDocument1=new Document();
            onlineDocument1.setDocumentCategory(onlineDocument.getDocumentCategory());
            onlineDocument1.setDocumentName(onlineDocument.getDocumentName());
            onlineDocument1.setDocumentNote(onlineDocument.getDocumentNote());
            onlineDocument1.setDocumentPath(onlineDocument.getDocumentPath());
            onlineDocument1.setDocumentVersion(onlineDocument.getDocumentVersion());
        }
        summaryService.saveOrUpdateSummaryJobData(summaryJobData, attachements1, onlineDocument1);
    }
    
    @Override
    public void autoSaveSummaryJobData(SummaryJobDataDTO summaryJobDataDTO)
            throws BusinessException, ParameterException, ConcurrencyException,
            TechnicalException {
        Assert.notNull(summaryJobDataDTO,"summaryJobData不能为null");
        SummaryJobData summaryJobData = DozerChangeUtil.changeBeforeToAfter(summaryJobDataDTO, SummaryJobData.class);
        //转换job
        if(summaryJobDataDTO.getJobDTO()!=null){
            Job job = taskManagementService.findJobDetailByJobId(summaryJobDataDTO.getJobDTO().getJobId());
            if(job.getActualEndTime() != null){
                return;
            }
             
            summaryJobData.setJob(job);
        }
        if(summaryJobDataDTO.getNextApproval()!=null){
            UserInfo nextApproval = DozerChangeUtil.changeBeforeToAfter(summaryJobDataDTO.getNextApproval(), UserInfo.class);
            summaryJobData.setNextApproval(nextApproval);
        }
       
        if(summaryJobDataDTO.getOnlineDocumentDTO()!=null){
            Document doc = DozerChangeUtil.changeBeforeToAfter(summaryJobDataDTO.getOnlineDocumentDTO(), Document.class);
            summaryJobData.setOnlineDocument(doc);
        }
        summaryService.autoSaveSummaryJobData(summaryJobData);
    }

    @Override
    public void deleteAllDocumentById(Long jobId) {
        Assert.notNull(jobId,"jobId不能为null");
        summaryService.deleteAllDocumentById(jobId);
    }
    @Override
    public List<DocumentDTO> dealOldAttachment(List<Long> oldDocumentIds){
        List<DocumentDTO> oldAttachments = new ArrayList<DocumentDTO>();
        if(oldDocumentIds!=null&&oldDocumentIds.size()!=0){
            for(Long id:oldDocumentIds){
                DocumentDTO docDTO = conferenceQueryWebService.findDocumentByDocumentId(id);
                if(docDTO!=null){
                    DocumentDTO docNoId = new DocumentDTO();
                    docNoId.setDocumentCategory(docDTO.getDocumentCategory());
                    docNoId.setDocumentName(docDTO.getDocumentName());
                    docNoId.setDocumentNote(docDTO.getDocumentNote());
                    docNoId.setDocumentPath(docDTO.getDocumentPath());
                    docNoId.setDocumentVersion(docDTO.getDocumentVersion());
                    oldAttachments.add(docNoId);
                }
            }
            
        }
        return oldAttachments;
    }
//    private List<DocumentDTO> dealOldAttachmentByJobId(Long jobId) throws BusinessException{
//        List<DocumentDTO> oldAttachments = new ArrayList<DocumentDTO>();
//        SummaryJobDataDTO summaryJobDataDTO = this.findPreSummaryJobDataByJobDataId(jobId);
//        List<DocumentDTO> preAttachments = summaryJobDataDTO.getAttachmentsDTOs();
//        if(preAttachments!=null&&preAttachments.size()!=0){
//            for(DocumentDTO docDTO:preAttachments){
//                    DocumentDTO docNoId = new DocumentDTO();
//                    docNoId.setDocumentCategory(docDTO.getDocumentCategory());
//                    docNoId.setDocumentName(docDTO.getDocumentName());
//                    docNoId.setDocumentNote(docDTO.getDocumentNote());
//                    docNoId.setDocumentPath(docDTO.getDocumentPath());
//                    docNoId.setDocumentVersion(docDTO.getDocumentVersion());
//                    oldAttachments.add(docNoId);
//            }
//            
//        }
//        return oldAttachments;
//    }
    

}
